package business_opportunity

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/business_opportunity/strategic_monitor"
	"github.com/gin-gonic/gin"
)

type StrategicMonitorController struct {
	controller.BaseController
	strategicMonitorService strategic_monitor.StrategicMonitorServiceIface
}

// NewStrategicMonitorController 租户用户战略监控控制器
func NewStrategicMonitorController(
	strategicMonitorService strategic_monitor.StrategicMonitorServiceIface,
) *StrategicMonitorController {
	return &StrategicMonitorController{
		strategicMonitorService: strategicMonitorService,
	}
}

func (c *StrategicMonitorController) RegisterRouter(g *gin.RouterGroup) {
	// 新增监控品类接口
	g.POST("strategic-monitor/industry-submit", c.industrySubmit)
	g.GET("strategic-monitor/industry-monitor-list", c.industryMonitorList)
	g.GET("strategic-monitor/industry-submit-list", c.industrySubmitList)
	g.POST("strategic-monitor/industry-monitor-change", c.industryMonitorChange)
	g.GET("strategic-monitor/industry-stat", c.industryStat)
	g.GET("strategic-monitor/fuzzy-search-industry", c.fuzzySearchIndustry)

	g.POST("strategic-monitor/company-submit", c.companySubmit)
	g.GET("strategic-monitor/company-monitor-list", c.companyMonitorList)
	g.GET("strategic-monitor/company-submit-list", c.companySubmitList)
	g.POST("strategic-monitor/company-monitor-change", c.companyMonitorChange)
	g.GET("strategic-monitor/company-stat", c.companyStat)
	g.GET("strategic-monitor/fuzzy-search-company", c.fuzzySearchCompany)
	g.GET("strategic-monitor/result-list", c.resultList)
}

func (c *StrategicMonitorController) industrySubmit(ctx *gin.Context) {
	request := &dto.SubmitIndustryRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	response, err := c.strategicMonitorService.SubmitIndustry(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) industryMonitorList(ctx *gin.Context) {
	request := &dto.GetIndustryMonitorListRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	response, err := c.strategicMonitorService.GetIndustryMonitorList(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) industrySubmitList(ctx *gin.Context) {
	request := &dto.GetIndustrySubmitListRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 20
	}
	response, err := c.strategicMonitorService.GetIndustrySubmitList(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) industryMonitorChange(ctx *gin.Context) {
	request := &dto.ChangeIndustryMonitorStatusRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	response, err := c.strategicMonitorService.ChangeIndustryMonitorStatus(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) industryStat(ctx *gin.Context) {
	request := &dto.GetIndustryStatRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	response, err := c.strategicMonitorService.GetIndustryStat(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) fuzzySearchIndustry(ctx *gin.Context) {
	request := &dto.FuzzySearchIndustryOrCompanyRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	response, err := c.strategicMonitorService.GetIndustryList(ctx, &dto.GetIndustryListRequest{
		Keyword:     request.Keyword,
		AuditStatus: []int32{2},
		Page:        1,
		PageSize:    10,
	})
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) companySubmit(ctx *gin.Context) {
	request := &dto.SubmitCompanyRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	response, err := c.strategicMonitorService.SubmitCompany(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) companyMonitorList(ctx *gin.Context) {
	request := &dto.GetCompanyMonitorListRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	response, err := c.strategicMonitorService.GetCompanyMonitorList(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) companySubmitList(ctx *gin.Context) {
	request := &dto.GetCompanySubmitListRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 20
	}
	response, err := c.strategicMonitorService.GetCompanySubmitList(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) companyMonitorChange(ctx *gin.Context) {
	request := &dto.ChangeCompanyMonitorStatusRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	response, err := c.strategicMonitorService.ChangeCompanyMonitorStatus(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) companyStat(ctx *gin.Context) {
	request := &dto.GetCompanyStatRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	response, err := c.strategicMonitorService.GetCompanyStat(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) fuzzySearchCompany(ctx *gin.Context) {
	request := &dto.FuzzySearchIndustryOrCompanyRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	response, err := c.strategicMonitorService.GetBidCompanyList(ctx, &dto.GetBidCompanyListRequest{
		Keyword:     request.Keyword,
		AuditStatus: []int32{2},
		Page:        1,
		PageSize:    10,
	})
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}

func (c *StrategicMonitorController) resultList(ctx *gin.Context) {
	request := &dto.GetMonitorResultRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	response, err := c.strategicMonitorService.GetMonitorResult(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, response)
}
